﻿using System;
using System.Collections.Generic;
using System.Data;

namespace SPDbFileExtractor.Framework.Entities
{
    /// <summary>
    /// List
    /// </summary>
    public class List
    {
        /// <summary>
        /// Gets or sets the list id.
        /// </summary>
        /// <value>
        /// The list id.
        /// </value>
        public Guid ListId { get; set; }

        /// <summary>
        /// Gets or sets the web id.
        /// </summary>
        /// <value>
        /// The web id.
        /// </value>
        public Guid WebId { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the created.
        /// </summary>
        /// <value>
        /// The created.
        /// </value>
        public DateTime Created { get; set; }

        /// <summary>
        /// Gets or sets the created by.
        /// </summary>
        /// <value>
        /// The created by.
        /// </value>
        public string CreatedBy { get; set; }

        /// <summary>
        /// Gets or sets the item count.
        /// </summary>
        /// <value>
        /// The item count.
        /// </value>
        public int ItemCount { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Title;
        }

        /// <summary>
        /// Gets the collection from table.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static List<List> GetCollectionFromTable(DataTable table)
        {
            if (table == null || table.Rows.Count == 0)
            {
                return new List<List>();
            }

            var result = new List<List>();

            foreach (DataRow row in table.Rows)
            {
                List list = new List();
                list.Created = (DateTime)row["Created"];
                list.CreatedBy = row["CreatedBy"] as string;
                list.Description = row["Description"] as string;
                list.ListId = new Guid(row["ListId"].ToString());
                list.Title = row["Title"] as string;
                list.WebId = new Guid(row["WebId"].ToString());
                list.ItemCount = (int)row["ItemCount"];

                result.Add(list);
            }

            return result;
        }
    }
}
